package cn.yx.common.security.webflux.manager;

import cn.yx.common.security.pojo.JwtUserDetails;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * <p>JWT Token认证管理器</p>
 *
 * @author Wgssmart
 */
@Component
@Primary
public class JwtTokenAuthenticationManager implements ReactiveAuthenticationManager {

    @Override
    public Mono<Authentication> authenticate(Authentication authentication) {
        return Mono.just(authentication)
                .map(auth -> (JwtUserDetails) auth.getPrincipal())
                .map(jwtUserDetails -> new UsernamePasswordAuthenticationToken(jwtUserDetails.getUsername(), null, jwtUserDetails.getAuthorities()));
    }
}